<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字游戏</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            background-color: #f0f8ff;
            margin: 0;
            padding: 0;
        }
        .container {
            max-width: 400px;
            margin: 100px auto;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #4CAF50;
        }
        input[type="number"] {
            padding: 10px;
            font-size: 16px;
            margin-top: 10px;
            border-radius: 4px;
            border: 1px solid #ccc;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            margin-top: 10px;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        .message {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
        }
        .error {
            color: red;
        }
        .success {
            color: green;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>猜数字游戏</h1>
        <p>请猜一个从 1 到 100 之间的数字：</p>
        <input type="number" id="guess" placeholder="输入你的猜测" min="1" max="100">
        <button onclick="checkGuess()">猜一猜！</button>

        <div class="message" id="resultMessage"></div>
    </div>

    <script>
        // 随机生成1到100之间的数字
        let targetNumber = Math.floor(Math.random() * 100) + 1;
        let attempts = 0;

        /**
         * 检查玩家的猜测
         */
        function checkGuess() {
            const guess = parseInt(document.getElementById('guess').value);
            const resultMessage = document.getElementById('resultMessage');
            attempts++;

            // 检查输入是否有效
            if (isNaN(guess) || guess < 1 || guess > 100) {
                resultMessage.textContent = "请输入一个有效的数字 (1-100)！";
                resultMessage.className = "message error";
                return;
            }

            // 判断玩家的猜测
            if (guess < targetNumber) {
                resultMessage.textContent = "猜的数字太小了！再试试吧！";
                resultMessage.className = "message";
            } else if (guess > targetNumber) {
                resultMessage.textContent = "猜的数字太大了！再试试吧！";
                resultMessage.className = "message";
            } else {
                resultMessage.textContent = `恭喜你，猜对了！你一共猜了 ${attempts} 次。`;
                resultMessage.className = "message success";
                // 游戏结束后重置游戏
                resetGame();
            }
        }

        /**
         * 重置游戏
         */
        function resetGame() {
            targetNumber = Math.floor(Math.random() * 100) + 1; // 重新生成一个目标数字
            attempts = 0; // 重置猜测次数
            document.getElementById('guess').value = ''; // 清空输入框
        }
    </script>
</body>
</html>
